package com.dunka.demo.controller;

import com.alibaba.fastjson.JSON;
import com.dunka.demo.pojo.Logo_type;
import com.dunka.demo.pojo.Menu;
import com.dunka.demo.service.MenuService;
import com.dunka.demo.util.RestResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Dunka
 * @Description 菜单控制
 * @Date 15:18   2019/7/25
 * @ClassName MenuController
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单管理相关接口",description = "提供菜单管理相关的RestAPI")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "获取所有菜单项",
            notes = "传入参数：" +
                    "无"+
                    "返回类型中Object 里存放的data 是List<Menu>",
            response = Object.class)
    @GetMapping("/getAll")
    public Object getAllMenu(){
        List<Menu> list = menuService.getAllMenu();
        if (list!=null){
            return RestResponseUtil.success(list);
        }else{
            return RestResponseUtil.notFound();
        }
    }

    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "修改菜单项",
            notes = "传入参数：" +
                    "@RequestBody Menu menu"+
                    "返回类型：" +
                    "Object 里存放的data 是boolean",
            response = Object.class)
    @PostMapping("/updateMenu")
    public Object updateMenu(@RequestBody Menu menu){
        if (menuService.updateMenu(menu)){
            return RestResponseUtil.success();
        }else{
//            此操作无效，返回无效码
            return RestResponseUtil.failure("修改菜单失败");
        }
    }

    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "删除菜单项",
            notes = "传入参数：" +
                    "@RequestParam(value = \"id\")  int id"+
                    "返回类型：" +
                    "Object 里存放的data 是boolean",
            response = Object.class)
    @DeleteMapping("/deleteMenu")
    public Object deleteMenu(@RequestParam(value = "id")  int id){
        if (menuService.deleteMenu(id)){
            return RestResponseUtil.success();
        }else{
            return RestResponseUtil.failure("删除菜单失败");
        }
    }

    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "增加菜单项",
            notes = "传入参数：" +
                    "@RequestBody Menu menu"+
                    "返回类型：" +
                    "Object 里存放的data 是boolean",
            response = Object.class)
    @PostMapping("/addMenu")
    public Object addMenu(@RequestBody Menu menu) throws Exception {
//        先检查菜单图标是否在枚举范围里
//        if (!Logo_type.contains(menu.getLogo_type().toString())){
//            throw new Exception();
//        }
        if (menuService.addMenu(menu)){
            return RestResponseUtil.success();
        }else{
            return RestResponseUtil.failure("添加菜单失败");
        }
    }

    @ApiImplicitParam(name = "GMC-ACCESS-TOKEN",value = "登录时得到的token",paramType = "header")
    @ApiOperation(value = "通过菜单id获取对应的菜单项",
            notes = "传入参数：" +
                    "@RequestParam(value = \"id\") int id"+
                    "返回类型：" +
                    "Object 里存放的data 是menu",
            response = Object.class)
    @GetMapping("/getMenuById")
    public Object getMenuById(@RequestParam(value = "id") int id){
//        System.out.println(id);
        Menu menu = menuService.getMenuById(id);
        List<Menu> list = new ArrayList<>();
        if (menu != null){
            list.add(menu);
            return RestResponseUtil.success(list);
        }else {
            return RestResponseUtil.notFound();
        }
    }
}
